var S_POOLS = {};
export const Utils = {
    isInArr(item, array) {
        for (let i = 0; i < array.length; i++) {
            if (item === array[i]) {
                return true;
            }
        }
        return false;
    },
    removeFromArr(item, array) {
        for (let i = 0; i < array.length; i++) {
            if (item === array[i]) {
                array.splice(i, 1);
            }
        }
    },
    setNUllFromArr(item, array){
        for (let i = 0; i < array.length; i++) {
            if (item === array[i]) {
                array[i] = null;
            }
        }
    },
    random: function (from, to) {
        var abs = Math.abs(to - from) + 1;
        var rand = Math.floor(Math.random() * abs);
        var min = Math.min(from, to);
        return (min + rand);
    },

    // 根据权重获取随机值，数组格式[[1,10],[2,10],[3,10]]  第一个是index，第二个是权重
    randomWeight(arr) {
        let tempArr = [];
        tempArr.push(0);
        let sum = 0;
        arr.forEach(element => {
            sum += element[1];
            tempArr.push(sum);
        });
        let random = Math.random() * sum;
        let index = 0;
        for (let i = tempArr.length - 1; i > 0; i--) {
            if (random >= tempArr[i]) {
                index = i;
                break;
            }
        }
        return arr[index][0];
    },

    getPoolNode(key) {
        if (!S_POOLS[key]) {
            return;
        }
        if (S_POOLS[key].pool.size() > 0) {
            return S_POOLS[key].pool.get();
        } else {
            let node = cc.instantiate(S_POOLS[key].prefab);
            return node;
        }
    },

    putPoolNode(key, node) {
        if (!S_POOLS[key]) {
            node.destroy();
            return;
        }

        S_POOLS[key].pool.put(node);
    },

    createPool: function (key, prefab, initCount) {
        if (!S_POOLS[key]) {
            let pool = {};
            pool.pool = new cc.NodePool();
            pool.prefab = prefab;

            for (let i = 0; i < initCount; ++i) {
                let node = cc.instantiate(prefab);
                pool.pool.put(node);
            }

            S_POOLS[key] = pool;
        }
    },

    createPoolEx: function (key, cb) {
        if (!S_POOLS[key]) {
            let pool = new cc.js.Pool(null, 10);
            pool.get = function () {
                return pool._get() || (cb && cb());
            };
            S_POOLS[key] = pool;
        }

        return S_POOLS[key];
    },

    destroyPool: function (key) {
        if (S_POOLS[key]) {
            S_POOLS[key].pool.clear();
            delete S_POOLS[key];
        }
    },

    clearAllPool: function () {
        for (var key in S_POOLS) {
            S_POOLS[key].clear();
            delete S_POOLS[key];
        }
    },

    scheduleOnce(cb, delay) {
        G.game.scheduleOnce(cb, delay);
    },
    schedule(cb, delay) {
        G.game.schedule(cb, delay);
    },
    unschedule(cb) {
        G.game.unschedule(cb);
    },

    prefixZero(num, n) {
        return (Array(n).join(0) + num).slice(-n);
    },

    copyToClipboard(str) {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {

        } else {
            var el = document.createElement("textarea");
            el.value = str;
            el.setAttribute('readonly', '');
            el.style.contain = 'strict';
            el.style.position = 'absolute';
            el.style.left = '-9999px';
            el.style.fontSize = '12pt';
            document.body.appendChild(el);

            var selected = false;
            var selection = document.getSelection();
            if (selection.rangeCount > 0) {
                selected = selection.getRangeAt(0);
            }
            el.select();
            document.execCommand('copy');
            document.body.removeChild(el);

            if (selected) {
                document.getSelection().removeAllRanges();
                document.getSelection().addRange(selected);
            }
        }
    },

    // 计算两点距离
    getDistance(p1, p2) {
        if (!p1 || !p2) {
            return 0;
        }
        p1.x = p1.x || 0;
        p1.y = p1.y || 0;
        p2.x = p2.x || 0;
        p2.y = p2.y || 0;
        let a = p1.x - p2.x;
        let b = p1.y - p2.y;

        return Math.sqrt(Math.round(a * a + b * b));
    },
}